import json
import requests
import socket

hostname = socket.gethostname()
ip_address = socket.gethostbyname(hostname)
print(ip_address)

while True:
    url = 'https://api.map.baidu.com/location/ip?ak=KHkVjtmfrM6NuzqxEALj0p8i1cUQot6Z'
    ip_response = requests.get(url)
    ip_response_dict = ip_response.json()
    print(ip_response_dict)
    province_city = ip_response_dict['content']
    print(province_city)
    location = input("请输入想要查找的城市城市名称：")
    response = requests.get(
        "http://api.map.baidu.com/telematics/v3/weather?location={}&output=json&ak=TueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQ&callback=?".format(location))
    # print(response)

    json_str = response.text
    # print(json_str)

    json_dict = json.loads(json_str)
    # print(json_dict)
    results_dict = json_dict['date']
    print(results_dict)
    results_dict = json_dict['results']
    # print(results_dict)

    for item_dict in results_dict:
        city = item_dict['currentCity']
        print(city)
        weather_data = item_dict['weather_data']
        # print(weather_data)
        pm25 = item_dict['pm25']
        print(pm25)
        index = item_dict['index']
        for x in range(len(index)):
            new_index = index[x]
            # print(new_index)
            des = index[x]['des']
            tipt = index[x]['tipt']
            title = index[x]['title']
            print("des: {}, tipt: {}, title:{}".format(des, tipt, title))
        for x in range(len(weather_data)):
            new_weather = weather_data[x]
            # print(new_weather)
            date = weather_data[x]['date']
            dayPictureUrl = weather_data[x]['dayPictureUrl']
            nightPictureUrl = weather_data[x]['nightPictureUrl']
            weather = weather_data[x]['weather']
            wind = weather_data[x]['wind']
            temperature = weather_data[x]['temperature']
            print("date:{}, dayPictureUrl:{}, nightPictureUrl:{}, weather:{}, wind:{},temperature:{} ".format(date, dayPictureUrl, nightPictureUrl, weather, wind, temperature))





